//
//  LSZDynamicInformationVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/8/18.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZDynamicInformationVC.h"
#import "LCUtils.h"

@interface LSZDynamicInformationVC ()<UITableViewDelegate,UITableViewDataSource>
//@property(nonatomic,strong) UICollectionView * collectionView;
@property(nonatomic,strong) NSArray * arrayName;
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,strong) NSMutableArray * mutArrayData;
@property(nonatomic,strong) LSZDynamicClassificationManager * classManager;

@property(nonatomic,strong) UITableView * tableViewAll;

@end

@implementation LSZDynamicInformationVC
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableViewAll reloadData];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}
-(void)initWithDataWithPage :(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic allDynamicLists:@"" id_id:@"" type:@"" fuid:@"" selfStr:@"0" theme:@"" page:[NSString stringWithFormat:@"%ld",(long)page] is_city:@"" is_follow:@"" hot:@"" :^(BOOL isOK, NSString * string, LSZAllDynamicListsManager * manager) {
        if (isOK == YES) {
            if (page == 1) {
                [weakSelf.mutArrayData  removeAllObjects];
                [weakSelf.mutArrayData addObjectsFromArray:manager.arrayData];
                if(manager.arrayData.count == 0){
                    weakSelf.tableViewAll.mj_footer.hidden = YES;
                }
            }else{
                [weakSelf.mutArrayData addObjectsFromArray:manager.arrayData];
                if(manager.arrayData.count == 0){
                    [weakSelf.tableViewAll.mj_footer endRefreshingWithNoMoreData];
                }
            }
            [weakSelf.tableViewAll reloadData];
        }else{
            //此处是错误信息
            [weakSelf initWithtring:string];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.mutArrayData = [[NSMutableArray alloc]init];
    self.tableViewAll = [[UITableView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight-TAB_BAR_HEIGHT-NAVIGATION_BAR_HEIGHT) style:UITableViewStylePlain];
    if (@available(iOS 11.0, *)) {
        self.tableViewAll.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableViewAll.delegate = self;
    self.tableViewAll.dataSource = self;
    self.tableViewAll.separatorColor = divider;
    self.tableViewAll.estimatedRowHeight =0;
    self.tableViewAll.estimatedSectionFooterHeight =0;
    self.tableViewAll.estimatedSectionHeaderHeight =0;
    self.tableViewAll.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
    [self.view addSubview:self.tableViewAll];
    self.page = 1;
    [self initWithDataWithPage:self.page];

    [self addMoreData];
    [self forClassification];
}
#pragma mark - 获取分类
-(void)forClassification
{
    [LSZRequestResolutionDic publishDynamicCategorization:^(BOOL isOk, NSString * string, LSZDynamicClassificationManager * ClassManager) {
        if (isOk == YES) {
            self.classManager = ClassManager;
            [self.tableViewAll reloadData];
        }
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
         if (self.classManager.mutMgArray.count%4 == 0) {
            return  ((ScreenWidth-0.001)/4+15)*((self.classManager.mutMgArray.count)/4);
        }else{
            return  ((ScreenWidth-0.001)/4+15)*((self.classManager.mutMgArray.count)/4+1);
        }
    }else{
        return 0.00001;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section == self.mutArrayData.count-1){
        return 20;
    }else{
        return 0.0001;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 0.0001;
    }else{
        LSZAllDynamicListsMg * mg = nil;
        mg = self.mutArrayData[indexPath.section-1];
        if(indexPath.row == 0){
            float A = [LSZSpecialPackaging initWithReturnsthesizeOf:13.5 :0.5];
            int B = [LSZSpecialPackaging initWithReturnsthesizeOf:70 :5];
            int C;
            if (ScreenWidth == 320) {
                C = 70;
            }else if(ScreenWidth == 375){
                C = 77;
            }else {
                C = 85;
            }
            NSString * string = [NSString stringWithFormat:@"%@%@",mg.theme_content,mg.content];
            CGFloat height;
            if ([string isEqualToString:@""]) {
                height = 0;
            }else{
                height = [LSZSpecialPackaging caculateHeightOfText:ScreenWidth-B height:80 font:A :string];
            }
            return  height-10+C;
        }else if(indexPath.row == 1){
            CGFloat heightA = [LSZSpecialPackaging initWithReturnsthesizeOf:60 :5];

            CGFloat wids = (ScreenWidth-30-heightA)/3;
            if(mg.arrayImages.count == 0){
                return  0.00001;
            }
            else if (mg.arrayImages.count == 1) {
                return (ScreenWidth-30-heightA)/2+10;
            }else{
                if(mg.arrayImages.count%3 == 0){
                    return (mg.arrayImages.count/3)*(wids+10)+10;
                }else{
                    return (mg.arrayImages.count/3+1)*(wids+10)+10;
                }
            }
        }else if (indexPath.row == 2){
            if (mg.resources_name.length ==0) {
                return 0.0001;
            }else{
                CGFloat size = [LSZSpecialPackaging initWithReturnsthesizeOf:50 :5];
                return size;
            }
        }
        return 80;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  self.mutArrayData.count+1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0){
        return 0;
    }else{
         return 4;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        LSZDynamicAreaHeadCell * cell = [[LSZDynamicAreaHeadCell alloc]initWithClassDataArray:self.classManager];
        cell.myCollectionView.pushBlock = ^(NSInteger rowNumber) {
            LSZDynamicClassificationMg * mg = nil;
            if(rowNumber != 0){
                mg = self.classManager.mutMgArray[rowNumber-1];
            }
            if(rowNumber == 0){//话题
                LSZListOfTopicsViewController * vc = [[LSZListOfTopicsViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
           else if((self.classManager.mutMgArray.count >6&&rowNumber == 7)||(self.classManager.mutMgArray.count <6&&rowNumber == self.classManager.mutMgArray.count+1)){
            //更多
               LSZDynamicOtherViewController * vc = [[LSZDynamicOtherViewController alloc]init];
               vc.images = mg.images;
               [self.navigationController pushViewController:vc animated:YES];
            }
            else{
                LSZDynamicRealTimeSelectionVC * vc = [[LSZDynamicRealTimeSelectionVC alloc]init];
                vc.kind = mg.id_id;//分类id
                vc.images = mg.images;
                vc.cname = mg.name;//分类
                vc.theme = @"";//话题id
                vc.themeStr = @"";//话题
                vc.typeDescription = mg.discrib;
                [self.navigationController pushViewController:vc animated:YES];
            }
        };
        return cell;
    }else{
        UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cellReusableHeader"];
        if (!cell) {
            cell = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"cellReusableHeader"];
        }
        return cell;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    if (self.mutArrayData.count+1 > indexPath.section) {
        LSZAllDynamicListsMg * mg = nil;
        mg = self.mutArrayData[indexPath.section-1];
        if (indexPath.row == 0) {
            LSZBasicInformationCollectionViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZBasicInformationCollectionViewCell" forIndexPath:indexPath];
            cell.AllDynamicListsMg = mg;
            [cell.butFocusOn tapGesture:^(UIGestureRecognizer *ges) {
                [self clickButFocusOnDetaill:mg.uid :indexPath.section :mg];
            }];
            [cell.imageHeader tapGesture:^(UIGestureRecognizer *ges) {
                LSZPersonalInformationPageVC * vc = [[LSZPersonalInformationPageVC alloc]init];
                vc.uid =mg.uid;
                vc.imageViewHeader = mg.avatar;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            return cell;
        }else if (indexPath.row == 1){
            LSZImageViewFocusOnTableViewCell * cell = [[LSZImageViewFocusOnTableViewCell alloc]initWithImageViewFocusOn:mg.arrayImages];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            return cell;
        }else if (indexPath.row == 2){
            if (mg.resources_name.length ==0) {
                UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
                if (!cell) {
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone ;
                return cell;
            }
            LSZAddMusicCollectionViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZAddMusicCollectionViewCell" forIndexPath:indexPath];
            cell.AllDynamicListsMg = mg;
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            return cell;
        }
        else{
            LSZThumbUpForwardCollectionViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZThumbUpForwardCollectionViewCell" forIndexPath:indexPath];
            cell.AllDynamicListsMg = mg;
            [cell.imageViewEvaluation tapGesture:^(UIGestureRecognizer *ges) {
                [self goToDynamicReview:mg];
            }];
            [cell.labelEvaluation tapGesture:^(UIGestureRecognizer *ges) {
                [self goToDynamicReview:mg];
            }];
            [cell.imageViewZan tapGesture:^(UIGestureRecognizer *ges) {
                [self initWiththeDynamicPointOfPraise:mg :indexPath.section];
            }];
            [cell.labelZan tapGesture:^(UIGestureRecognizer *ges) {
                [self initWiththeDynamicPointOfPraise:mg :indexPath.section];
            }];
            [cell.labelShare tapGesture:^(UIGestureRecognizer *ges) {
                [weakSelf labelShareClickWithMg:mg section:indexPath.section];
            }];
            [cell.imageViewShare tapGesture:^(UIGestureRecognizer *ges) {
                [weakSelf labelShareClickWithMg:mg section:indexPath.section];
            }];
            [cell.butMore tapGesture:^(UIGestureRecognizer *ges) {
                [self chooseToReport:mg];
            }];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            return cell;
        }
    }else{
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone ;
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0||indexPath.row == 1) {
        LSZAllDynamicListsMg * mg = nil;
        mg = self.mutArrayData[indexPath.section-1];
        LSZDynamicCommentDetailsVC * vc = [[LSZDynamicCommentDetailsVC alloc]init];
        vc.AllDynamicListsMg = mg;
        [self.navigationController pushViewController:vc animated:YES];
    }
   else  if (indexPath.row == 2) {
        LSZAllDynamicListsMg * mg = nil;
        mg = self.mutArrayData[indexPath.section-1];
        NSString * str = [[NSString alloc]init];
        NSString * id_id_str = [[NSString alloc]init];
        if ([mg.type isEqualToString:@"2"]) {
            str = @"/Music/get_url";
        }else if ([mg.type isEqualToString:@"5"]){
            str = @"/Music/artist";
        }else{
            str = @"/Music/album";
        }
        id_id_str = mg.resources_id;
        [LSZRequestResolutionDic dynamicGetsAlbumArtistSongDataBasedOn:id_id_str URL:str type:mg.type :^(BOOL isOk, NSString * string, LSZNewAudioHomepageManager * manager) {
            if (isOk == YES) {
                if ([mg.type isEqualToString:@"2"]) {
                    NSLog(@"音频播放");
                    [LCUtils lcListPlayWithLSZNewAudioHomepageManager:manager];
                }else{
                    LSZNewAudioPlayImageAndTableViewVC * vc = [[LSZNewAudioPlayImageAndTableViewVC alloc]init];
                    LSZNewAudioHomepageMg * audioMg = nil;
                    audioMg = manager.arrayData[0];
                    [LCUtils albumArtistDetailsInterfaceJump:vc audioMg:audioMg typeComeWhere:@"1"];
                    vc.lastPageNumberRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
                    vc.lastSection = [NSString stringWithFormat:@"%ld",(long)indexPath.section-1];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
        }];
    }
}
#pragma mark - 选择举报
-(void)chooseToReport:(LSZAllDynamicListsMg *)manager
{
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
        return;
    }
    ACActionSheet *actionSheet = [[ACActionSheet alloc] initWithTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"举报"] actionSheetBlock:^(NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            LSZReportViewController * VC = [[LSZReportViewController alloc]init];
            VC.name = manager.username;
            VC.titleString = manager.content;
            VC.tid = manager.id_id;
            VC.type = @"1";
            VC.comment_id = @"";
            [self.navigationController pushViewController:VC animated:YES];
        }
    }];
    [actionSheet show];
}

#pragma mark - 进入动态评论
-(void)goToDynamicReview :(LSZAllDynamicListsMg *)mg
{
    LSZDynamicCommentDetailsVC * vc = [[LSZDynamicCommentDetailsVC alloc]init];
    vc.AllDynamicListsMg = mg;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 对动态点赞
-(void)initWiththeDynamicPointOfPraise :(LSZAllDynamicListsMg *)mg :(NSInteger)section
{
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
        return;
    }
    NSString * ck = [[NSString alloc]init];
    NSString * mode = [[NSString alloc]init];
    if ([mg.is_admire isEqualToString:@"0"]) {
        ck = @"0";//点赞
        mode = @"";
     }else{
         ck = @"";//取消点赞
         mode = @"1";
    }
    [LSZRequestData theDynamicPointOfPraise:mg.id_id type:@"1" ck:ck mode:mode :^(BOOL isOk, NSString * string, NSDictionary * dic) {
        if (isOk == YES) {
            if([ck isEqualToString:@"0"]){
                mg.is_admire = @"1";
                mg.admire_num = [NSString stringWithFormat:@"%ld",(long)([mg.admire_num integerValue]+1)];
                [self initWithtring:@"点赞成功!"];
            }else{
                mg.is_admire = @"0";
                mg.admire_num = [NSString stringWithFormat:@"%ld",(long)([mg.admire_num integerValue]-1)];
                [self initWithtring:@"取消点赞!"];
            }
            LSZThumbUpForwardCollectionViewCell * cell = (LSZThumbUpForwardCollectionViewCell *)[self.tableViewAll cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3  inSection:section]];

            if([mode isEqualToString:@"1"]){
                cell.imageViewZan.image = [UIImage imageNamed:@"点赞"];

            }else{
                cell.imageViewZan.image = [UIImage imageNamed:@"我的_签到_留言_已点赞"];
            }
            cell.labelZan.text = mg.admire_num;
        }else{
            [self initWithtring:string];
        }
    }];
}
#pragma mark - 关注
-(void)clickButFocusOnDetaill :(NSString *)uid :(NSInteger)section :(LSZAllDynamicListsMg *)mg
{
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
    }else{
        [LSZRequestData payAttentionToOthers:uid Ck:@"0" mode:@"0" :^(BOOL isOk, NSString * str, NSDictionary * dic) {
            if (isOk == YES) {
                [self initWithtring:@"关注成功!"];
                LSZBasicInformationCollectionViewCell * cell = (LSZBasicInformationCollectionViewCell *)[self.tableViewAll cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0  inSection:section]];
                [cell.butFocusOn setTitle:@"已关注" forState:UIControlStateNormal];
                cell.butFocusOn.backgroundColor =  partialShallow;
                cell.butFocusOn.userInteractionEnabled = NO;
            }else{
                [self initWithtring:str];
            }
        }];
    }
}
-(void)addMoreData
{
    self.tableViewAll.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableViewAll registerClass:[LSZBasicInformationCollectionViewCell class] forCellReuseIdentifier:@"LSZBasicInformationCollectionViewCell"];
    [self.tableViewAll registerClass:[LSZAddMusicCollectionViewCell class] forCellReuseIdentifier:@"LSZAddMusicCollectionViewCell"];
    [self.tableViewAll registerClass:[LSZThumbUpForwardCollectionViewCell class] forCellReuseIdentifier:@"LSZThumbUpForwardCollectionViewCell"];
    self.tableViewAll.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self initWithDataWithPage :self.page];
            [self.tableViewAll.mj_header endRefreshing];
        });
    }];
    self.tableViewAll.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self initWithDataWithPage:self.page];
            [self.tableViewAll.mj_footer endRefreshing];
        });
    }];
}
#pragma mark - 弹出警告框
-(void)initWithtring :(NSString *)string
{
    AlertKuangView * alert = [AlertKuangView new];
    [alert moveJingGaoKuang:string:self.view];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
#pragma mark - 赠送
- (void)labelShareClickWithMg:(LSZAllDynamicListsMg *)mg section:(NSInteger)section
{
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
        return;
    }
    LCSendSugarDemo6 *sendSugar = [[LCSendSugarDemo6 alloc] init];
    sendSugar.mg = mg;
    CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 40, 360);
    sendSugar.frame = rect;
    [sendSugar.undoButton addTarget:self action:@selector(undoButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
    @weakify(self)
    [sendSugar.sugarListBut tapGesture:^(UIGestureRecognizer *ges) {
        @strongify(self)
        [self sugarListButClickWithMg:mg];
    }];
    sendSugar.successBlock = ^{
        @strongify(self)
        [self.zh_popupController dismiss];
    };
    self.zh_popupController = [zhPopupController new];
    self.zh_popupController.layoutType = zhPopupLayoutTypeCenter;
    self.zh_popupController.maskAlpha = 0.4;
    [self.zh_popupController presentContentView:sendSugar duration:0.25 springAnimated:false];
}
- (void)undoButtonClick
{
    [self.zh_popupController dismiss];
}
- (void)sugarListButClickWithMg:(LSZAllDynamicListsMg *)mg
{
    [self.zh_popupController dismiss];
    LCSugarListTableViewController *viewController = [[LCSugarListTableViewController alloc] init];
    viewController.mg = mg;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        viewController.title = @"赠送列表";
    });
    [self.navigationController pushViewController:viewController animated:YES];
}
@end
